<?php

class pagerController extends Controller {

    public $initphp_list = array(
        'edit',
        'save',
        'del');


    /**
     *
     * @var pagerService
     */
    private $pagerService;

    public function __construct() {
        parent::__construct();
        $this->checkLogin();
        $this->pagerService = InitPHP::getService("pager");
    }

    public function run() {
        $data = $this->pagerService->getAll();
        $this->view->assign("menu_now", "pager");
        $this->view->assign("data", $data);
        $this->view->set_tpl("manage/header", "F"); // 设置公共头部模板
        $this->view->set_tpl("manage/pager/list");
        $this->view->set_tpl("manage/footer", 'L'); // 设置公共尾部模板
        $this->view->assign("title", "网页管理-凯巴斯-管理后台");
        $this->view->display();
    }

    public function edit() {
        $this->view->assign("menu_now", "pager");
        $id = $this->controller->get_gp("id");
        if ($id > 0) {
            $data = $this->pagerService->getOne($id);
            $pid = $data['pid'];
            $this->view->assign("id", $id);
            $this->view->assign("data", $data);
        } else {
            $pid = $this->controller->get_gp("pid");
        }
        $parent = $this->pagerService->getOne($pid);
        $this->view->assign("parent", $parent);
        $this->view->set_tpl("manage/header", "F"); // 设置公共头部模板
        $this->view->set_tpl("manage/pager/add");
        $this->view->set_tpl("manage/footer", 'L'); // 设置公共尾部模板
        $this->view->assign("title", "添加导航-凯巴斯-管理后台");
        $this->view->display();
    }

    public function del() {
        $id = $this->controller->get_gp("id");
        $this->pagerService->delete($id);
        JsMessage("删除成功！", "/manage/pager/run");
    }

    public function save() {
        $id = $this->controller->get_gp("id");
        $data = $this->controller->get_gp(array(
            'title',
            'seo_title',
            'descript',
            'keyword',
            'content',
            'update_time',
            'sort'));
        $data['update_time'] = strtotime($data['update_time']);
        if ($data['descript']==""){
            $data['descript']=utf8_substr($data['content'], 128);
        }
        if ($id) {
            $this->pagerService->update($id, $data);
        } else {
            $data['add_time'] = time();
            $this->pagerService->add($data);
        }
        JsMessage("保存成功！", '/manage/pager/run');
    }

    /**
     * 检查登陆状态
     */
    public function checkLogin() {
        $session = $this->getUtil('session');
        $adminid = $session->get('adminid');
        if (empty($adminid)) {
            $function = $this->getLibrary('function');
            $function->goto_url('/manage/login');
            die();
        }
        $this->view->assign("adminname", $session->get('adminname'));
        $this->view->assign("adminid", $session->get('adminid'));
    }
}